package com.navcoo.yidaaddproapi.service.serviceImpl;

import cn.hutool.core.util.NumberUtil;
import com.navcoo.yidaaddproapi.constants.NumberConstants;
import com.navcoo.yidaaddproapi.mapper.UserMapper;
import com.navcoo.yidaaddproapi.pojo.dao.UserDAO;
import com.navcoo.yidaaddproapi.pojo.dto.user.CreateUserDTO;
import com.navcoo.yidaaddproapi.response.Result;
import com.navcoo.yidaaddproapi.response.exception.ResultStatus;
import com.navcoo.yidaaddproapi.service.UserService;
import com.navcoo.yidaaddproapi.utils.DESUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 用户服务Impl
 *
 * @author Navcoo
 * @date 2024-04-24
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Value("${encrypt-key}")
    private String encryptKey;

    /**
     * 创建用户
     *
     * @param userEmail 用户电子邮件
     * @param userName  用户名
     * @param password  密码
     * @param avatar    用户头像
     * @return {@link Result}<{@link String}>
     */
    @Override
    public Result<String> createUser(String userEmail, String userName, String password, String avatar) {
        log.info("创建用户");

        String passwordEncrypt = DESUtil.encrypt(password, encryptKey);

        UserDAO createUserDAO = UserDAO.builder()
                .userEmail(userEmail)
                .userName(userName)
                .passwordEncrypt(passwordEncrypt)
                .avatar(avatar)
                .build();
        log.info("创建用户参数，createUserDAO:{}", createUserDAO);
        // 插入
        Integer insert = userMapper.insert(createUserDAO);
        log.info("成功插入{}行", insert);
        if (NumberUtil.equals(insert, NumberConstants.ZERO)) {
            log.info("用户ID:{}", createUserDAO.getUserId());
            return Result.success(createUserDAO.getUserId());
        } else {
            log.error("创建用户失败！");
            return Result.failure(ResultStatus.ERROR_PARAM);
        }

    }

    /**
     * 删除用户
     *
     * @param userName 用户名
     * @param password 密码
     */
    @Override
    public void deleteUser(String userName, String password) {
        log.info("删除用户");
    }

    /**
     * 更新用户信息
     *
     * @param userName 用户名
     * @param password 密码
     */
    @Override
    public void updateUserInfo(String userName, String password) {
        log.info("更新用户信息");
    }

    /**
     * 获取用户信息
     *
     * @param userName 用户名
     * @param password 密码
     */
    @Override
    public void getUserInfo(String userName, String password) {
        log.info("获取用户信息");
    }
}
